import CryptoJS from "crypto-js";




export default {
    getAES(data) { //加密
        let key = '8d5945b7-fcb2-36';  //密钥
        let iv = 'ff4-bd6ec587ca4c';
        let encrypted = this.getAesString(data, key, iv); //密文
        let encrypted1 = CryptoJS.enc.Utf8.parse(encrypted);
        return encrypted;
    },

    getAesString(data, key, iv) {
        key = CryptoJS.enc.Utf8.parse(key);
        iv = CryptoJS.enc.Utf8.parse(iv);
        let encrypted = CryptoJS.AES.encrypt(data, key,
            {
                iv: iv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.Pkcs7
            });
        return encrypted.toString();    //返回的是base64格式的密文
    },

    getDAes(data) {//解密
        let decryptedStr = null;
        if (data === null || data === undefined) {
            return null
        } else {
            let key = $g.AESkey;  //密钥
            let iv = $g.AESiv;
            decryptedStr = this.getDAesString(data, key, iv);
        }
        return decryptedStr;
    },
    getDAesString(encrypted, key, iv) {
        key = CryptoJS.enc.Utf8.parse(key);
        iv = CryptoJS.enc.Utf8.parse(iv);
        let decrypted = CryptoJS.AES.decrypt(encrypted, key,
            {
                iv: iv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.Pkcs7
            });
        return decrypted.toString(CryptoJS.enc.Utf8);
    }
}
